package com.auxgroup.smarthome.enterprise.service;

import com.auxgroup.smarthome.enterpriseapi.EnterpriseApi;
import com.auxgroup.smarthome.enterpriseapi.requestbody.BigDataHistoryBody;
import com.auxgroup.smarthome.enterpriseapi.requestbody.GetDeviceActiveDetailBody;
import com.auxgroup.smarthome.enterpriseapi.responsebody.*;
import com.auxgroup.smarthome.web.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 设备报表
 * Created by Toby Lai on 2017/5/4.
 */
@Service
public class DeviceReportService {
    @Autowired
    private ProductService productService;

    /**
     * 获取实时报表
     * @param pk
     * @param gid
     * @return
     */
    public ApiResponse<RealtimeReportReponse> getDeviceRtReport(String pk, String gid) {
        RealtimeReportReponse resultResponse = EnterpriseApi.DeviceReport.getDeviceRealtimeReport(productService.getToken(pk), pk, gid);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 获取激活报表
     * @param pk
     * @param gid
     * @return
     */
    public ApiResponse<DeviceActivationReportBody> getActivationReport(String pk, String  gid) {
        DeviceActivationReportBody resultResponse = EnterpriseApi.DeviceReport.getDeviceActivationReport(productService.getToken(pk), pk,  gid);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 激活历史报表（大数据）
     * @param pk
     * @param bigDataHistoryBody
     * @return
     */
    public ApiResponse<ActivationHistoryReport> getDeviceActivationHistoryReport(String pk, BigDataHistoryBody bigDataHistoryBody) {
        ActivationHistoryReport resultResponse = EnterpriseApi.DeviceReport.getActivationHistoryReport(productService.getToken(pk), pk, bigDataHistoryBody);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 获取活跃设备报表
     * @param pk
     * @param gid
     * @return
     */
    public ApiResponse<DeviceLivenessReportReponse> getLivenessDeviceReport(String pk, String gid) {
        DeviceLivenessReportReponse resultResponse = EnterpriseApi.DeviceReport.getDeviceLivenessDeviceReport(productService.getToken(pk), pk, gid);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 获取活跃历史报表（大数据）
     * @param pk
     * @param bigDataHistoryBody
     * @return
     */
    public ApiResponse<LivenessHistoryReport> getLivenessDeviceHistoryReport(String pk, BigDataHistoryBody bigDataHistoryBody) {
        LivenessHistoryReport resultResponse = EnterpriseApi.DeviceReport.getLivenessHistoryReport(productService.getToken(pk), pk, bigDataHistoryBody);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 设备概览
     * @param pk
     * @return
     */
    public ApiResponse<DeviceSurvey> getSummaryReport(String pk) {
        DeviceSurvey resultResponse = EnterpriseApi.DeviceReport.getDeviceSummary(productService.getToken(pk), pk);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 设备活跃详情
     * @param pk
     * @param getDeviceActiveDetailBody
     * @return
     */
    public ApiResponse<DeviceActiveDetails> getDeviceActiveDetail(String pk, GetDeviceActiveDetailBody getDeviceActiveDetailBody) {
        DeviceActiveDetails resultResponse = EnterpriseApi.DeviceReport.getDeviceActiveDetail(productService.getToken(pk), pk, getDeviceActiveDetailBody);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 设备新增详情
     * @param pk
     * @param getDeviceActiveDetailBody
     * @return
     */
    public ApiResponse<DeviceNewAddDetails> getDeviceNewAddDeatils(String pk, GetDeviceActiveDetailBody getDeviceActiveDetailBody) {
        DeviceNewAddDetails resultResponse = EnterpriseApi.DeviceReport.getDeviceNewAddReport(productService.getToken(pk), pk, getDeviceActiveDetailBody);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 活跃周期查询
     * @param pk
     * @return
     */
    public ApiResponse<ActiveCycle> getActiveCycleReport(String pk) {
        ActiveCycle resultResponse = EnterpriseApi.DeviceReport.getActiveCycleReport(productService.getToken(pk), pk);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 链接时长查询
     * @param pk
     * @return
     */
    public ApiResponse<ConnectionDurationResponse> getConnectionDuration(String pk) {
        ConnectionDurationResponse resultResponse = EnterpriseApi.DeviceReport.getConnectionDurationReport(productService.getToken(pk), pk);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 获取故障台数报表
     * @param pk
     * @param gid
     * @return
     */
    public ApiResponse<FaultAndAlertQuantityReportResponse> getFaultDeviceReport(String pk, String gid) {
        FaultAndAlertQuantityReportResponse resultResponse = EnterpriseApi.DeviceReport.getDeviceFaultReport(productService.getToken(pk), pk, gid);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 获取报警设备台数报表
     * @param pk
     * @param gid
     * @return
     */
    public ApiResponse<FaultAndAlertQuantityReportResponse> getAlertDeviceReport(String pk, String gid) {
        FaultAndAlertQuantityReportResponse resultResponse = EnterpriseApi.DeviceReport.getDeviceAlertReport(productService.getToken(pk), pk, gid);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 获取故障次数报表
     * @param pk
     * @param gid
     * @return
     */
    public ApiResponse<FaultAndAlertEventReportResponse> getFaultEventFrequency(String pk, String gid) {
        FaultAndAlertEventReportResponse resultResponse = EnterpriseApi.DeviceReport.getDeviceFaultFrequency(productService.getToken(pk), pk, gid);
        if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
            return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
        }
        return ApiResponse.ok(resultResponse);
    }

    /**
     * 获取报警次数报表
     * @param pk
     * @param gid
     * @return
     */
   public ApiResponse<FaultAndAlertEventReportResponse> getAlertEventFrequency(String pk, String gid) {
       FaultAndAlertEventReportResponse resultResponse = EnterpriseApi.DeviceReport.getDeviceAlertFrequency(productService.getToken(pk), pk, gid);
       if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
           return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
       }
       return ApiResponse.ok(resultResponse);
   }

    /**
     * 获取自定义事件历史报表
     * 获取自定义事件历史报表
     * 日报表最多查询 30 天。
     * 周报表最多查询 53 周。
     * 月报表最多查询 12 月。
     * 日期查询说明
     * cycle为date时，start,end的格式是年-月-日，example:2016-08-01,
     * cycle为week时，start,end的格式是年-周，example:2016-51
     * cycle为month时，start,end的格式为年-月，example:2016-12
     * @param pk
     * @param gid
     * @param cycle
     * @param start
     * @param end
     * @return
     */
   public ApiResponse<DeviceHistoryDetails> getCustomEventReport(String pk, String gid, String cycle, String start, String end) {
       DeviceHistoryDetails resultResponse = EnterpriseApi.DeviceReport.getDeviceCustomHistory(productService.getToken(pk), pk, gid, cycle, start, end);
       if(StringUtils.isNotEmpty(resultResponse.getError_code())) {
           return ApiResponse.prompt(resultResponse.getError_code(), resultResponse.getError_message());
       }
       return ApiResponse.ok(resultResponse);
   }


}
